package com.mouse.hxsj.service;

import com.mouse.damo.micro.*;
import com.mouse.haoi.entity.Point;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.constants.SystemParams;
import com.mouse.hxsj.entity.enums.GoodsType;
import com.mouse.hxsj.entity.vo.Goods;
import com.mouse.hxsj.entity.vo.ShopVo;
import com.mouse.hxsj.util.DateUtil;
import com.mouse.hxsj.util.JsonUtil;
import com.mouse.hxsj.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ShopService {
    private static CustomService customService = new CustomService();
    private static int typeIndex = -1;

    public static void main(String[] args) {
        Com com = new Com();
        FindStr findStr = new FindStr(com);
        Mouse mouse = new Mouse(com);
        FindPic findPic = new FindPic(com);
        com.setResourcePath("D:\\damo");
        com.setDict(5, "商店.txt");
        com.setDict(6, "价格.txt");
        int hwnd = 6949736;
        boolean bind = com.bind(hwnd);
        System.out.println("绑定窗口" + bind);
        // 查询窗口宽高
        Window window = new Window(com);
        int[] windowRect = window.getClientSize(hwnd);
        System.out.println("窗口宽高" + JsonUtil.toJson(windowRect));
        int height = windowRect[2], width = windowRect[1];
//        FindPic findPic = new FindPic(com);
//        int[] pic = findPic.findPic(0, 0, height, height,
//                "login/摆摊查询.bmp", "203040", 0.9, 0);
//        System.out.println(JsonUtil.toJson(pic));
//        if (pic[0] == -1) findNPC(com, hwnd, height, width);

//        findType(com, height, width);
        List<Goods> goodsList = trunGoods(com, height, width);
        for (Goods goods : goodsList) {
            buyGoods(com, goods, height, width, null);
        }
    }

    public static boolean buyGoods(Com com, Goods goods, int height, int width, List<ShopVo> shopList) {
        Mouse mouse = new Mouse(com);
        FindPic findPic = new FindPic(com);
        FindStr findStr = new FindStr(com);
        String name = goods.getName();
        Integer price = goods.getPrice();
        String color = goods.getColor();
        if (name == null || price == null) return false;
//        if (!name.contains("地煞元气袋") || price.compareTo(111) != 0) return;
        String name1 = name.replaceAll("\\+", "").replaceAll("(.+)(\\d+)", "$1");
        if (StringUtil.isBlank(name1)) return false;
        String name2 = name.replaceAll("\\+", "").replaceAll("(.+)(\\d+)", "$2");
        boolean match = shopList.stream().anyMatch(shopVo -> {
            String matchType = shopVo.getMatchType();
            if ("模糊".equals(matchType)) {
                if (!"全部".equals(shopVo.getColor()) && !shopVo.getColor().equals(color)) return false;
                if (!name1.contains(shopVo.getName())) return false;
                //无视等级1 直接匹配价格
                if (shopVo.getGrade1() <= 0) return price.compareTo(shopVo.getPrice()) <= 0;
                //有等级1
                if (StringUtil.isBlank(name2)) return false;
                //商品等级不能为空
                String grade = name2.replaceAll("[^0-9]+", "");
                //商品等级为数字
                if (!grade.matches("[0-9]+")) return false;
                //匹配等级 匹配价格
                if (shopVo.getGrade1() <= Integer.valueOf(grade)) {
                    return price.compareTo(shopVo.getPrice()) <= 0;
                }
            } else if ("精确".equals(matchType)) {
                if (!"全部".equals(shopVo.getColor()) && !shopVo.getColor().equals(color)) return false;
                return shopVo.getName().equals(name) && price.compareTo(shopVo.getPrice()) <= 0;
            }
            return false;
        });
        if (!match) return false;
        File file = new File(com);
        String picName = SystemParams.sourcePath + "/shop/" + name1 + " " + "price " + DateUtil.toDateStr(new Date(), "yyyy-MM-dd HH-mm-ss") + ".png";
        file.screenImage(0, 0, width, height, picName);

        mouse.mouseClick(goods.getX(), goods.getY(), false);
        //默认数量为1
        boolean maxFlag = true;
        customService.loopActin(300, () -> {
            int[] pic = findPic.findPic(0, 0, width, height, "login/max.bmp", "203040", 0.9, 0);
            if (pic[0] != -1) {
                if (maxFlag) {
                    mouse.mouseClick(pic[1] + 10, pic[2] + 5, true);
                    Sleep.sleep(100);
                }
                mouse.mouseClick(pic[1], pic[2] - 18, true);
                return true;
            }
            return false;
        });
        Sleep.sleep(100);
        Point concalPoint = customService.loopPoint(5, () -> new Point(findPic.findPic(0, 0, width, height, "login/取消.bmp", "203040", 0.9, 0)));
        int x1 = concalPoint.getX() - 320, y1 = concalPoint.getY() - 70, x2 = concalPoint.getX() + 40, y2 = concalPoint.getY();
        String priceStr = findStr.readStr(x1, y1, x2, y2, "ffd800-000000|f6f1ba-000000|ffffff-000000", "", 0.9, 6);
        String numStr = findStr.readStr(x1, y1, x2, y2, "00ffc0-000000", "", 0.9, 6);
        System.out.println(priceStr);
        if (StringUtil.isBlank(priceStr)) return false;
        if (StringUtil.isBlank(numStr)) return false;
        Integer thisPrice = StringUtil.toInt(priceStr.replaceAll(",", ""));
        if (!price.equals(thisPrice / Integer.parseInt(numStr.replaceAll(",", "")))) return false;
        mouse.mouseClick(concalPoint.getX() - 54, concalPoint.getY() + 5, true);
        return true;
    }

    //收起种类+
    public static void closeType(Com com) {
        Window window = new Window(com);
        int[] windowRect = window.getClientSize(com.getHwnd());
        int height = windowRect[2], width = windowRect[1];

        Mouse mouse = new Mouse(com);
        FindPic findPic = new FindPic(com);
        while (true) {
            int[] pic = findPic.findPic(0, 0, height, width, "login/减.bmp", "203040", 0.8, 0);
            if (pic[0] == -1) break;
            mouse.mouseClick(pic[1], pic[2], true);
            Sleep.sleep(150);
        }
    }

    //查找类型
    public static void findType(Com com, int height, int width, String type1, String type2) {
        FindPic findPic = new FindPic(com);
        Mouse mouse = new Mouse(com);
        Point point1 = customService.loopPoint(50, () -> new Point(
                findPic.findPic(0, 0, width, height, "login/摆摊查询.bmp", "203040", 0.9, 0)));
        System.out.println(point1);
        List<Integer> typeList = GoodsType.getType(type1, type2);
        int x = point1.getX() - 258, y = point1.getY() + 60 + typeList.get(0) * 26, y2 = point1.getY() + 79 + typeList.get(0) * 26;
        mouse.mouseClick(x, y, true);
        Sleep.sleep(200);
        mouse.mouseDoubleClick(x, y2 + typeList.get(1) * 18 + 4);
    }

    public static List<Goods> trunGoods(Com com, int height, int width) {
        FindStr findStr = new FindStr(com);
        List<List<Integer>> ypList = findStr.findStrEx(0, 0, width, height, "银票",
                "fef2b4-000000", 0.9, 5);
        System.out.println(JsonUtil.toJson(ypList));
        List<Goods> goodsList = new ArrayList<>();
        for (List<Integer> list : ypList) {
            if (list.get(0) == -1) continue;
            //名称
            int nameX1 = list.get(1) - 314, nameY1 = list.get(2) - 9;
            int nameX2 = list.get(1) - 232, nameY2 = list.get(2) + 18;
            String nameStr = findStr.readStr(nameX1, nameY1, nameX2, nameY2,
                    "ffffff-000000|5296fc-000000|28f049-000000|" +
                            "ea35ff-000000|e53cfe-000000|2a97fd-000000|30e926-000000|28f049-000000",
                    "", 0.9, 5);
            //颜色
            int colorX1 = list.get(1) - 216, colorY1 = list.get(2) - 9;
            int colorX2 = list.get(1) - 188, colorY2 = list.get(2) + 18;

            String colorStr = findStr.readStr(colorX1, colorY1, colorX2, colorY2,
                    "ffffff-000000|5296fc-000000|28f049-000000|" +
                            "ea35ff-000000|e53cfe-000000|2a97fd-000000|30e926-000000|28f049-000000",
                    "", 0.9, 5);
            //价格
            int priceX1 = list.get(1) - 74, priceY1 = list.get(2) - 9;
            int priceX2 = list.get(1) - 5, priceY2 = list.get(2) + 18;
            String priceStr = findStr.readStr(priceX1, priceY1, priceX2, priceY2,
                    "f6f1ba-000000|ffd800-000000|fef2b4-000000", "", 0.9, 6);
            if (StringUtil.isBlank(priceStr)) continue;
            String price = priceStr.replaceAll(",", "");
            if (StringUtil.isBlank(price)) continue;
            Goods goods = new Goods(nameStr, colorStr, Integer.parseInt(price)
                    , "1", "2");
            goods.setX(list.get(1) - 335);
            goods.setY(list.get(2));
            goodsList.add(goods);
            System.out.println(nameStr + " " + priceStr + " " + colorStr);
        }
        return goodsList;
    }

    public static void findNPC(Com com, int hwnd, int height, int width) {
        FindStr findStr = new FindStr(com);
        Mouse mouse = new Mouse(com);
        Press press = new Press(com);


        Point point = findMap(com, 2, height, width);
        if (point.getState() == -1) {
            press.keyPress(Press.M);
            Sleep.sleep(500);
            point = findMap(com, 10, height, width);
        }
        Sleep.sleep(100);
        System.out.println(JsonUtil.toJson(point));
        mouse.mouseDoubleClick(point.getX() + 361, point.getY() + 408);
        Sleep.sleep(100);
        press.keyPress(Press.M);
        Point point1 = customService.loopPoint(50, () -> new Point(
                findStr.findStrE(0, 0, height, height, "摆摊查询",
                        "000000-000000", 0.9, 5)));
        mouse.mouseDoubleClick(point1.getX(), point1.getY() + 5);
        System.out.println(point1);
        //
        Sleep.sleep(500);
    }

    public static Point findMap(Com com, long timeout, int height, int width) {
        FindPic findPic = new FindPic(com);
        Point point = customService.loopPoint(timeout, () -> {
            int[] pic = findPic.findPic(0, 0, width, height,
                    "login/场景地图.bmp", "203040", 0.8, 0);
            return new Point(pic[0], pic[1], pic[2]);
        });
        return point;
    }
}
